package sync_protocol

import "gddgame.cc/galaxy/common/feature/orbit"

//go:generate msgp

type SyncMode byte

const (
	_ SyncMode = iota
	SingleSyncMode
	AreaSyncMode
	ClusterSyncMode
)

//msgp:tuple AreaCreateRequest
type AreaCreateRequest struct {
	AutoClose bool `json:"auto_close" msg:"auto_close"`
	Save      bool `json:"save" msg:"save"`
}

//msgp:tuple AreaCloseRequest
type AreaCloseRequest struct {
	Token string `json:"token" msg:"token"`
}

//msgp:tuple AreaConfig
type AreaConfig struct {
	Area           string   `json:"area" msg:"area"`
	SyncMode       SyncMode `json:"sync_mode" msg:"sync_mode"`
	MaxVision      int32    `json:"max_vision" msg:"max_vision"`
	Gap            int32    `json:"gap" msg:"gap"`
	Rate           int32    `json:"rate" msg:"rate"`
	UpdateLocation bool     `json:"update_location" msg:"update_location"`
}

//msgp:tuple AreaUpdateLocation
type AreaUpdateLocation struct {
	OpenId   string `json:"open_id" msg:"open_id"`
	AreaId   string `json:"area_id" msg:"area_id"`
	Location []byte `json:"location" msg:"location"`
}

//msgp:tuple AddItemRequest
type AddItemRequest struct {
	Items []Item              `json:"items" msg:"items"`
	Block orbit.BlockLocation `json:"block" msg:"block"`
	Area  string              `json:"area" msg:"area"`
}

//msgp:tuple AddTimeRequest
type AddTimeRequest struct {
	Times []TimeInfo          `json:"times" msg:"times"`
	Block orbit.BlockLocation `json:"block" msg:"block"`
	Area  string              `json:"area" msg:"area"`
}

// localid byte, location []byte, level byte, meta []interface{}, info []interface{}, data []interface{}, ownerId string, belong string, agent string
//msgp:tuple Item
type Item struct {
	Id       byte          `json:"id" msg:"id"`
	Location []byte        `json:"location" msg:"location"`
	Level    byte          `json:"level" msg:"level"`
	Meta     []interface{} `json:"meta" msg:"meta"`
	Info     []interface{} `json:"info" msg:"info"`
	Data     []interface{} `json:"data" msg:"data"`
	OwnerId  string        `json:"owner_id" msg:"owner_id"`
	Belong   string        `json:"belong" msg:"belong"`
	Agent    string        `json:"agent" msg:"agent"`
}

type TimeType byte

// code byte location []byte info...
// trigger: [time int, code byte, payload []]
// move: [startTime int, endTime int, payload [], position [], index int]
//msgp:tuple TimeInfo
type TimeInfo struct {
	Code     TimeType      `json:"code" msg:"code"`
	Location []byte        `json:"location" msg:"location"`
	Info     []interface{} `json:"info" msg:"info"`
}

func NewTriggerTime(location []byte, time int, code byte, payload []byte) TimeInfo {
	t := TimeInfo{
		Code:     TriggerTime,
		Location: location,
		Info:     []interface{}{time, code, payload},
	}
	return t
}

func NewMoveTime(location []byte, start int, end int, payload []byte, position [][]byte) TimeInfo {
	t := TimeInfo{
		Code:     MoveTime,
		Location: location,
		Info:     []interface{}{start, end, payload, position, 0},
	}
	return t
}

func NewProcessTime(location []byte, start int, end int, payload []byte) TimeInfo {
	t := TimeInfo{
		Code:     ProcessTime,
		Location: location,
		Info:     []interface{}{start, end, payload},
	}
	return t
}
